package org.swim.session;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.ejb3.annotation.RemoteBinding;
import org.swim.entity.Ability;
import org.swim.entity.HelpRequests;

import org.swim.entity.User;


@Stateless
@RemoteBinding(jndiBinding="HelpRequestManagerRemote")
public class HelpRequestManager implements HelpRequestManagerRemote {

	/**
	 * Manager degli entity beans. Permette di interagire direttamente col database inserendo e ricercando dati
	 */
	@PersistenceContext(unitName="swim") private EntityManager manager;
	
	@Override
	public int createNewHelpRequest(int senderID, int recipientID, int abilityID, String text) {
		
		HelpRequests toDefine=new HelpRequests();
		
		User sender=manager.find(User.class, senderID);
		User recipient=manager.find(User.class, recipientID);
		Ability ability=manager.find(Ability.class, abilityID);
		
		long dataMilli = System.currentTimeMillis(); 
		Date data = new Date( dataMilli );
		
		toDefine.setAbilita(ability);
		toDefine.setDatainvio(data);
		toDefine.setCompletata(false);
		toDefine.setMittente(sender);
		toDefine.setDestinatario(recipient);
		toDefine.setTesto(text);
		
		manager.persist(toDefine);
		
		return toDefine.getIdRichiesta();
	}

	@Override
	public void approveHelpRequest(int requestID) {
		
		HelpRequests toApprove=new HelpRequests();
		
		long dataMilli = System.currentTimeMillis(); 
		Date data = new Date( dataMilli );
		
		toApprove=manager.find(HelpRequests.class, requestID);
		toApprove.setDataAccettazione(data);

		
	}

	@Override
	public void deleteHelpRequest(int requestID) {
		
		HelpRequests rh = new HelpRequests();
		rh = manager.find(HelpRequests.class, requestID);
		if(rh != null)
			manager.remove(rh);	
		
	}

	@Override
	public void insertFeedback(String feedback, int requestID) {
		
		HelpRequests rh=new HelpRequests();
		
		rh=manager.find(HelpRequests.class, requestID);
		
		rh.setCompletata(true);
		rh.setFeedback(feedback);
		
	}

	@Override
	public List<HelpRequests> getSentApprovedRequest(int userID) {
		
		List<HelpRequests> result = new ArrayList<HelpRequests>();
		
		try 
		{
			User MITTENTE=manager.find(User.class, userID);

			
			Query q = manager.createQuery("SELECT OBJECT(rh) FROM HelpRequests AS rh " +
											"WHERE rh.dataAccettazione!=null AND rh.mittente=:U");
			q.setParameter("U", MITTENTE);
			
			
			@SuppressWarnings("unchecked")
			Collection<HelpRequests> coll = (Collection<HelpRequests>) q.getResultList();

			Iterator<HelpRequests> itar = coll.iterator();
			
			while (itar.hasNext())
			{	
				result.add(itar.next());
			}
			
		} 
		catch (EntityNotFoundException exc){} 
		
		return result;		
	}


	@Override
	public List<HelpRequests> getSentSuspendedRequest(int userID) {
		
		List<HelpRequests> result = new ArrayList<HelpRequests>();
		
		try 
		{
			User MITTENTE=manager.find(User.class, userID);

			
			Query q = manager.createQuery("SELECT OBJECT(rh) FROM HelpRequests AS rh " +
											"WHERE rh.dataAccettazione=null AND rh.mittente=:U");
			q.setParameter("U", MITTENTE);

			
			@SuppressWarnings("unchecked")
			Collection<HelpRequests> coll = (Collection<HelpRequests>) q.getResultList();

			Iterator<HelpRequests> itar = coll.iterator();
			
			while (itar.hasNext())
			{	
				result.add(itar.next());
			}
			
		} 
		catch (EntityNotFoundException exc){} 
		
		return result;		
	}

	@Override
	public List<HelpRequests> getRecievedSuspendedRequest(int userID) {
		
		List<HelpRequests> result = new ArrayList<HelpRequests>();
		
		try 
		{
			User DESTINATARIO=manager.find(User.class, userID);

			Query q = manager.createQuery("SELECT OBJECT(rh) FROM HelpRequests AS rh " +
											"WHERE rh.dataAccettazione=null AND rh.destinatario=:U");
			
			q.setParameter("U", DESTINATARIO);

			
			
			@SuppressWarnings("unchecked")
			Collection<HelpRequests> coll = (Collection<HelpRequests>) q.getResultList();

			Iterator<HelpRequests> itar = coll.iterator();
			
			while (itar.hasNext())
			{	
				result.add(itar.next());
			}
			
		} 
		catch (EntityNotFoundException exc){} 
		
		return result;		
	}

	@Override
	public List<HelpRequests> getRecievedApprovedRequest(int userID) {
		
		List<HelpRequests> result = new ArrayList<HelpRequests>();
		
		try 
		{
			User DESTINATARIO=manager.find(User.class, userID);

			Query q = manager.createQuery("SELECT OBJECT(rh) FROM HelpRequests AS rh " +
											"WHERE rh.dataAccettazione!=null AND rh.destinatario=:U " +
											"AND rh.completata=false");
			q.setParameter("U", DESTINATARIO);

			
			
			@SuppressWarnings("unchecked")
			Collection<HelpRequests> coll = (Collection<HelpRequests>) q.getResultList();

			Iterator<HelpRequests> itar = coll.iterator();
			
			while (itar.hasNext())
			{	
				result.add(itar.next());
			}
			
		} 
		catch (EntityNotFoundException exc){} 
		
		return result;		
		
	}

	@Override
	public List<HelpRequests> getRecievedCompletedRequest(int userID) {
	
		List<HelpRequests> result = new ArrayList<HelpRequests>();
		
		try 
		{
			User DESTINATARIO=manager.find(User.class, userID);

			Query q = manager.createQuery("SELECT OBJECT(rh) FROM HelpRequests AS rh " +
											"WHERE rh.completata=true AND rh.destinatario=:U");
			q.setParameter("U", DESTINATARIO);

			
			
			@SuppressWarnings("unchecked")
			Collection<HelpRequests> coll = (Collection<HelpRequests>) q.getResultList();

			Iterator<HelpRequests> itar = coll.iterator();
			
			while (itar.hasNext())
			{	
				result.add(itar.next());
			}
			
		} 
		catch (EntityNotFoundException exc){} 
		
		return result;		
	}

	@Override
	public HelpRequests getRequestByID(int requestID) {
		HelpRequests rh = new HelpRequests();
		rh = manager.find(HelpRequests.class, requestID);
		
		return rh;
	}



}
